package com.coin.exchange.okex.rest.future.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.coin.exchange.ok.vo.OkFutureBatchTradeVO;
import com.coin.exchange.ok.vo.OkFutureCancelVO;
import com.coin.exchange.ok.vo.OkFutureDevolveVO;
import com.coin.exchange.ok.vo.OkFutureExplosiveVO;
import com.coin.exchange.ok.vo.OkFutureHoldAmountVO;
import com.coin.exchange.ok.vo.OkFutureKlineVO;
import com.coin.exchange.ok.vo.OkFutureOrderInfoVO;
import com.coin.exchange.ok.vo.OkFuturePositionVO;
import com.coin.exchange.ok.vo.OkFuturePriceLimitVO;
import com.coin.exchange.ok.vo.OkFutureTradeVO;
import com.coin.exchange.ok.vo.OkFutureTradesHisVO;
import com.coin.exchange.ok.vo.OkFutureUserinfoVO;
import com.coin.exchange.okcoin.rest.HttpUtil;
import com.coin.exchange.okcoin.rest.HttpUtilManager;
import com.coin.exchange.okcoin.rest.MD5Util;
import com.coin.exchange.okcoin.rest.StringUtil;
import com.coin.exchange.okex.rest.future.IFutureRestApi;

/**
 * 新版本期货 REST API实现
 * 
 * @author zc
 * 
 */
@Service
public class OkexFutureRestApi implements IFutureRestApi {

	@Value("${okex.api.url}")
	private String url_prex;

//	public FutureRestApiV1(String url_prex,String api_key, String secret_key) {
//		this.api_key = api_key;
//		this.secret_key = secret_key;
//		this.url_prex = url_prex;
//	}
//
//	public FutureRestApiV1(String url_prex) {
//		this.url_prex = url_prex;
//
//	}

	/**
	 * 期货行情URL
	 */
	private final  String FUTURE_TICKER_URL = "/api/v1/future_ticker.do";
	/**
	 * 期货指数查询URL
	 */
	private final String FUTURE_INDEX_URL = "/api/v1/future_index.do";

	/**
	 * 期货交易记录查询URL
	 */
	private final  String FUTURE_TRADES_URL = "/api/v1/future_trades.do";

	/**
	 * 期货市场深度查询URL
	 */
	private final String FUTURE_DEPTH_URL = "/api/v1/future_depth.do";
	/**
	 * 美元-人民币汇率查询URL
	 */
	private final  String FUTURE_EXCHANGE_RATE_URL = "/api/v1/exchange_rate.do";

	private final String FUTURE_ESTIMATED_PRICE_URL = "/api/v1/future_estimated_price.do";
	private final String FUTURE_KLINE_URL = "/api/v1/future_kline.do";
	private final String FUTURE_HOLD_AMOUNT_URL = "/api/v1/future_hold_amount.do";
	private final String FUTURE_PRICE_LIMIT_URL = "/api/v1/future_price_limit.do";
	
	/**
	 * 期货取消订单URL
	 */
	private final  String FUTURE_CANCEL_URL = "/api/v1/future_cancel.do";

	/**
	 * 期货下单URL
	 */
	private final  String FUTURE_TRADE_URL = "/api/v1/future_trade.do";

	/**
	 * 期货账户信息URL
	 */
	private final String FUTURE_USERINFO_URL = "/api/v1/future_userinfo.do";
	
	/**
	 * 逐仓期货账户信息URL
	 */
	private final String FUTURE_USERINFO_4FIX_URL = "/api/v1/future_userinfo_4fix.do";

	/**
	 * 期货持仓查询URL
	 */
	private final String FUTURE_POSITION_URL = "/api/v1/future_position.do";
	
	/**
	 * 期货逐仓持仓查询URL
	 */
	private final String FUTURE_POSITION_4FIX_URL = "/api/v1/future_position_4fix.do"; 

	/**
	 * 用户期货订单信息查询URL
	 */
	private final String FUTURE_ORDER_INFO_URL = "/api/v1/future_order_info.do";
	
	private final String FUTURE_TRADES_HISTORY_URL = "/api/v1/future_trades_history.do";
	private final String FUTURE_BATCH_TRADE_URL = "/api/v1/future_batch_trade.do";
	private final String FUTURE_EXPLOSIVE_URL = "/api/v1/future_explosive.do";
	private final String FUTURE_DEVOLVE_URL = "/api/v1/future_devolve.do";

	@Override
	public String future_ticker(String symbol, String contractType) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", symbol);
		params.put("contract_type", contractType);
		String result = HttpUtil.get(url_prex + FUTURE_TICKER_URL, params);
		// System.out.println(result);
		return result;
	}

	@Override
	public String future_index(String symbol) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", symbol);
		String result = HttpUtil.get(url_prex + FUTURE_INDEX_URL, params);
		// System.out.println(result);
		return result;
	}

	@Override
	public String future_trades(String symbol, String contractType) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", symbol);
		params.put("contract_type", contractType);
		String result = HttpUtil.get(url_prex + FUTURE_TRADES_URL, params);
		// System.out.println(result);
		return result;
	}

	@Override
	public String future_depth(String symbol, String contractType) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", symbol);
		params.put("contract_type", contractType);
		String result = HttpUtil.get(url_prex + FUTURE_DEPTH_URL, params);
		return result;
	}

	@Override
	public String exchange_rate() throws Exception {
		String result = HttpUtil.get(url_prex + FUTURE_EXCHANGE_RATE_URL, null);
		return result;
	}

	@Override
	public String future_estimated_price(String symbol) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", symbol);
		String result = HttpUtil.get(url_prex + FUTURE_ESTIMATED_PRICE_URL, params);
		return result;
	}

	@Override
	public String future_kline(OkFutureKlineVO vo) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", vo.getSymbol());
		params.put("type", vo.getType());
		params.put("contract_type", vo.getContract_type());
		params.put("size", vo.getSize());
		params.put("since", vo.getSince());
		String result = HttpUtil.get(url_prex + FUTURE_KLINE_URL, params);
		return result;
	}

	@Override
	public String future_hold_amount(OkFutureHoldAmountVO vo) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", vo.getSymbol());
		params.put("contract_type", vo.getContract_type());
		String result = HttpUtil.get(url_prex + FUTURE_HOLD_AMOUNT_URL, params);
		return result;
	}

	@Override
	public String future_price_limit(OkFuturePriceLimitVO vo) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("symbol", vo.getSymbol());
		params.put("contract_type", vo.getContract_type());
		String result = HttpUtil.get(url_prex + FUTURE_PRICE_LIMIT_URL, params);
		return result;
	}
	
	@Override
	public String future_cancel(OkFutureCancelVO vo) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		if (!StringUtil.isEmpty(vo.getContract_type() )) {
			params.put("contract_type", vo.getContract_type());
		}
		if (!StringUtil.isEmpty(vo.getOrder_id() )) {
			params.put("order_id", vo.getOrder_id());
		}
		if (!StringUtil.isEmpty(vo.getApi_key() )) {
			params.put("api_key", vo.getApi_key());
		}
		if (!StringUtil.isEmpty(vo.getSymbol() )) {
			params.put("symbol", vo.getSymbol());
		}
		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());

		params.put("sign", sign);
		// 发送post请求

		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex,FUTURE_CANCEL_URL, params);
		// System.out.println(result);
		return result;

	}

	@Override
	public String future_trade(OkFutureTradeVO vo) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		if (!StringUtil.isEmpty(vo.getSymbol() )) {
			params.put("symbol", vo.getSymbol());
		}
		if (!StringUtil.isEmpty(vo.getContract_type() )) {
			params.put("contract_type", vo.getContract_type());
		}
		if (!StringUtil.isEmpty(vo.getApi_key() )) {
			params.put("api_key", vo.getApi_key());
		}
		if (!StringUtil.isEmpty(vo.getPrice())) {
			params.put("price", vo.getPrice());
		}
		if (!StringUtil.isEmpty(vo.getAmount())) {
			params.put("amount", vo.getAmount());
		}
		if (!StringUtil.isEmpty(vo.getType())) {
			params.put("type", vo.getType());
		}
		if (!StringUtil.isEmpty(vo.getMatch_price())) {
			params.put("match_price", vo.getMatch_price());
		}
		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		// 发送post请求

		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex,this.FUTURE_TRADE_URL, params);
		// System.out.println(result);
		return result;
	}

	@Override
	public String future_userinfo(OkFutureUserinfoVO vo) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();

		params.put("api_key", vo.getApi_key());

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		
		// 发送post请求

		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex,this.FUTURE_USERINFO_URL,
				params);
		// System.out.println(result);
		return result;
	}
	
	@Override
	public String future_userinfo_4fix(OkFutureUserinfoVO vo) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		    params.put("api_key", vo.getApi_key());

		    String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		    params.put("sign", sign);
		
		
		// 发送post请求

		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex,this.FUTURE_USERINFO_4FIX_URL,
				params);
		// System.out.println(result);
		return result;
	}

	@Override
	public String future_position(OkFuturePositionVO vo) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		if (!StringUtil.isEmpty(vo.getSymbol())) {
			params.put("symbol", vo.getSymbol());
		}
		if (!StringUtil.isEmpty(vo.getContract_type())) {
			params.put("contract_type", vo.getContract_type());
		}
		params.put("api_key", vo.getApi_key());
		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		// 发送post请求

		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex,this.FUTURE_POSITION_URL,
				params);
		// System.out.println(result);
		return result;

	}
	
	@Override
	public String future_position_4fix(OkFuturePositionVO vo) throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		if (!StringUtil.isEmpty(vo.getSymbol())) {
			params.put("symbol", vo.getSymbol());
		}
		if (!StringUtil.isEmpty(vo.getContract_type())) {
			params.put("contract_type", vo.getContract_type());
		}
		params.put("api_key", vo.getApi_key());
		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		// 发送post请求
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex,this.FUTURE_POSITION_4FIX_URL,
				params);
		// System.out.println(result);
		return result;
	}

	@Override
	public String future_order_info(OkFutureOrderInfoVO vo)
			throws HttpException, IOException {
		// 构造参数签名
		Map<String, String> params = new HashMap<String, String>();
		if (!StringUtil.isEmpty(vo.getContract_type())) {
			params.put("contract_type", vo.getContract_type());
		}
		if (!StringUtil.isEmpty(vo.getCurrent_page())) {
			params.put("current_page", vo.getCurrent_page());
		}
		if (!StringUtil.isEmpty(vo.getOrder_id())) {
			params.put("order_id", vo.getOrder_id());
		}
		if (!StringUtil.isEmpty(vo.getApi_key())) {
			params.put("api_key", vo.getApi_key());
		}
		if (!StringUtil.isEmpty(vo.getPage_length())) {
			params.put("page_length", vo.getPage_length());
		}
		if (!StringUtil.isEmpty(vo.getSymbol())) {
			params.put("symbol", vo.getSymbol());
		}
		if (!StringUtil.isEmpty(vo.getStatus())) {
			params.put("status", vo.getStatus());
		}
		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		// 发送post请求

		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex,this.FUTURE_ORDER_INFO_URL,
				params);
		// System.out.println(result);
		return result;

	}

	@Override
	public String future_trades_history(OkFutureTradesHisVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("date", vo.getDate());
		params.put("since", vo.getSince().toString());
		
		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.FUTURE_TRADES_HISTORY_URL, params);
		return result;
	}

	@Override
	public String future_batch_trade(OkFutureBatchTradeVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("contract_type", vo.getContract_type());
		params.put("orders_data", vo.getOrders_data());
		if (!StringUtil.isEmpty(vo.getLever_rate())) {
			params.put("lever_rate", vo.getLever_rate());
		}

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.FUTURE_BATCH_TRADE_URL, params);
		return result;
	}

	@Override
	public String future_explosive(OkFutureExplosiveVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("contract_type", vo.getContract_type());
		params.put("status", vo.getStatus());
		if (vo.getCurrent_page() != null) {
			params.put("current_page", vo.getCurrent_page().toString());
		}
		if (vo.getPage_number() != null) {
			params.put("page_number", vo.getPage_number().toString());
		}
		if (vo.getPage_length() != null) {
			params.put("page_length", vo.getPage_length().toString());
		}
		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.FUTURE_EXPLOSIVE_URL, params);
		return result;
	}

	@Override
	public String future_devolve(OkFutureDevolveVO vo) throws HttpException, IOException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("api_key", vo.getApi_key());
		params.put("symbol", vo.getSymbol());
		params.put("type", vo.getType());
		params.put("amount", vo.getAmount());

		String sign = MD5Util.buildMysignV1(params, vo.getSecret_key());
		params.put("sign", sign);
		
		HttpUtilManager httpUtil = HttpUtilManager.getInstance();
		String result = httpUtil.requestHttpPost(url_prex, this.FUTURE_DEVOLVE_URL, params);
		return result;
	}

}
